package com.gdufe.seckill.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdufe.common.utils.PageUtils;
import com.gdufe.common.utils.Query;

import com.gdufe.seckill.dao.SessionDao;
import com.gdufe.seckill.entity.SessionEntity;
import com.gdufe.seckill.service.SessionAdminService;


@Service("SessionAdminService")
public class SessionAdminServiceImpl extends ServiceImpl<SessionDao, SessionEntity> implements SessionAdminService {

    @Autowired
    private SessionDao sessionDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        int pageNum = Integer.parseInt((String) params.get("page"));
        //获取用户输入的查询条件
        String key = (String) params.get("key");
        //若条件非空，则对商品id或名字进行筛选
        QueryWrapper<SessionEntity> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(key)) {
            queryWrapper.eq("id", key).or().like("name", key);
        }

        IPage<SessionEntity> page = this.page(
                new Query<SessionEntity>().getPage(params),
                queryWrapper
        );
        params.put("page", pageNum);
        return new PageUtils(page);
    }

    @Override
    public List<SessionEntity> getSessions() {
        //获取所有场次信息
        List<SessionEntity> sessions = new ArrayList<>();
        QueryWrapper<SessionEntity> wrapper = new QueryWrapper<>();
        wrapper.groupBy("id");
        sessions =  sessionDao.selectList(wrapper);


        return sessions;
    }

}